﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2022 kingerrobot
// 版权所有。 
//
// 文件名：CuringEmsIntoTankImpl
// 文件功能描述：EMS小车任务生成
//
// 创建者：名字 (zzl)
// 时间：2022/1/20 15:15:16
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion

using K8.CBB;
using KingerRobot.MexicoSailunMDC.Dao;
//using KingerRobot.MexicoSailunMES.Dao;
using KingerRobot.MexicoSailunMES.Spi.DTO;
using System;
using System.Data;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace KingerRobot.MexicoSailunMDC.SpiService;

//[Logging]
public class CuringEmsIntoTankImpl : ICuringEmsIntoTankService
{
    private readonly static IWcsLocSku _WcsLocSku = new BWcsLocSku();
    private readonly static ITaskCmd _TaskCmd = new BTaskCmd();
    private readonly static ITask _Task = new BTask();
    private readonly static ITaskService _TaskService = new TaskImpl();
    private readonly static IWcsNetInCuring _WcsNetInCuring = new BWcsNetInCuring();
    private readonly static IRcsEquipEms _RcsEquipEms = new BRcsEquipEms();
    private readonly static IWmsOutOrderSummary _WmsOutOrderSummary = new BWmsOutOrderSummary();
    private readonly static IWmsOutOrderMaster _WmsOutOrderMaster = new BWmsOutOrderMaster();
    private readonly static IRcsLocation _RcsLocation = new BRcsLocation();
    private readonly static IWcsEquipLoc _WcsEquipLoc = new BWcsEquipLoc();
    private readonly static IRcsEquipCuring _RcsEquipCuring = new BRcsEquipCuring();
    private readonly static IRcsEquip _RcsEquip = new BRcsEquip();
    private readonly static IWcsAsrsNetOutPut _WcsAsrsNetOutPut = new BWcsAsrsNetOutPut();
    private readonly static IAbnormal _Abnormal = new BAbnormal();
    private readonly static IWcsNetAbnormal _WcsNetAbnormal = new BWcsNetAbnormal();
    private readonly static IProdProduct _ProdProduct = new BProdProduct();
    private readonly static IProdMaterial _ProdMaterial = new BProdMaterial();
    private readonly static IWms2MesService _Wms2MesService = new Wms2MesImpl();
    private readonly static IProdSkuProduct _skuProduct = new BProdSkuProduct();
    private static List<EmsTasksBean> _CacheTask = new List<EmsTasksBean>();
    private readonly static IWmsSetting _WmsSetting = new BWmsSetting();
    private readonly static IWcsAbnormalEmsHalt _WcsAbnormalEmsHalt = new BWcsAbnormalEmsHalt();
    private readonly static IMesProduct _MesProduct = new BMesProduct();



    public EmsTaskResult CreateTaskV2(long locIdA, long locIdB, long equipIdA, long equipIdB)
    {
        EmsTaskResult result = new EmsTaskResult()
        {
            CmdIdA = 0,
            CmdIdB = 0,
        };
        WriteLog($"ems创建任务开始000,locidA{locIdA},locIdB{locIdB}", true);


        // 位置绑定胎胚

        var skuA = GetWcsLocSkuByLocId(locIdA);
        var skuB = GetWcsLocSkuByLocId(locIdB);
        //WriteLog($"ems创建任务开始64,skuA{skuA.ToJson()}", true);
        var skuIdA = skuA?.SkuId ?? 0;
        var skuIdB = skuB?.SkuId ?? 0;
        WriteLog($"ems创建任务开始67,skuIdA{skuIdA},skuIdB{skuIdB}", true);
        //如果两个位置都没有绑定信息 则跳出  TODO 在此处加超 时根据wcsskutime
        if (skuIdA == 0 && skuIdB == 0)
        {
            WriteLog($"ems创建任务开始71,如果两个位置都没有绑定信息", true);
            return result;
        }



        //如果是单胎 判断超时时间
        if (skuIdA == 0 || skuIdB == 0)
        {

            var hasSku = skuIdA != 0 ? skuIdA : skuIdB;
            //当前车取货位的获取任务
            var hasTask = GetTaskByLoc(hasSku);
            if (hasTask is not null)
            {
                var hasTargetLoc = hasTask?.TargetLoc ?? 0;

                // 获取可用小车数量小车

                var hasEquipEms = _RcsEquipEms.GetEquipEmssByTargetLoc(hasTargetLoc);
                WriteLog($"ems创建任务开始257", true);
                if (hasEquipEms.Count == 0)
                {
                    WriteLog($"ems创建任务开始260", true);
                    result.CmdIdA = -2;
                    result.CmdIdB = -2;
                    return result;
                }
                else if (hasEquipEms.Count == 2)
                {
                    WriteLog($"ems创建任务开始77", true);
                    var locSku = skuA is not null ? skuA : skuB;
                    var bindTime = locSku.CreateTime ?? DateTime.MinValue;
                    //计算等待时长
                    _ = int.TryParse(_WmsSetting.GetWmsSettingByCode(ConstFields.EmsInputWait).Value, out var emsInputWait);
                    //未超时 直接跳出
                    if (DateTime.Now <= bindTime.AddSeconds(emsInputWait))
                    {
                        return result;
                    }
                }
            }
        }

        WriteLog($"ems创建任务开始89", true);
        // 判断小车有没有任务
        var taskCmdA = _TaskCmd.GetExecutableTaskCmdByEquipId(equipIdA);
        var taskCmdB = _TaskCmd.GetExecutableTaskCmdByEquipId(equipIdB);
        WriteLog($"ems创建任务开始93,taskCmdA{taskCmdA is null},taskCmdb{taskCmdB is null}", true);

        //如果每个有胎的位置都有任务 则跳出
        if (skuIdA != 0 && skuIdB != 0)
        {
            WriteLog($"ems创建任务开始98", true);
            if (taskCmdA is not null && taskCmdB is not null)
            {
                result.CmdIdA = taskCmdA.Id ?? 0;
                result.CmdIdB = taskCmdB.Id ?? 0;
                return result;
            }
        }
        else if (skuIdA != 0)
        {
            WriteLog($"ems创建任务开始106", true);
            if (taskCmdA is not null)
            {
                WriteLog($"ems创建任务开始109  1111返回{taskCmdA.Id ?? 0}", true);
                //var cmd = taskCmdA.Id ?? 0;
                result.CmdIdA = taskCmdA.Id ?? 0;
                return result;
            }
            //WriteLog($"ems创建任务开始110 值为{taskCmdA.Id ?? 0}", true);
        }
        else if (skuIdB != 0)
        {
            WriteLog($"ems创建任务开始114", true);
            if (taskCmdB is not null)
            {
                result.CmdIdB = taskCmdB.Id ?? 0;
                return result;
            }
        }


        ////如果最少一个车有任务了 则返回
        //if (taskCmdA is not null || taskCmdB is not null)
        //{
        //    return (taskCmdA.Id ?? 0, taskCmdB.Id ?? 0);
        //}




        //当前车取货位的获取任务
        var taskA = GetTaskByLoc(skuIdA);
        // 获取旁边取货位任务
        var taskB = GetTaskByLoc(skuIdB);
        //如果绑定了但是没主任务 则生成异常任务
        WriteLog($"ems创建任务开始136,taskA{taskA is null},taskB{taskB is null}", true);
        var abnormalCmdIdA = 0L;
        var abnormalCmdIdB = 0L;
        if (skuIdA != 0 && taskA == null)
        {
            //WriteLog($"ems创建任务开始141", true);
            //taskA = new POWcsTask() { SkuId = skuIdA };
            //abnormalCmdIdA = SetAbnormal(locIdA, equipIdA, taskA, "任务未找到[无法处理请料].");
            WriteLog($"ems创建任务开始144,A任务未找到[无法处理请料]", true);
            ////_WcsLocSku.UpdateWcsLocSkuTask(skuIdA, abnormalCmdIdA);
            //WriteLog($"ems创建任务开始146", true);
            //abnormalCmdIdA = abnormalCmdIdA == 0 ? -3 : abnormalCmdIdA;
            //WriteLog($"ems创建任务开始148,abnormalCmdIdA{abnormalCmdIdA}", true);
            return result;
        }
        if (skuIdB != 0 && taskB == null)
        {
            //WriteLog($"ems创建任务开始152", true);
            //taskB = new POWcsTask() { SkuId = skuIdB };
            //abnormalCmdIdB = SetAbnormal(locIdB, equipIdB, taskB, "任务未找到[无法处理请料].");
            WriteLog($"ems创建任务开始157,B任务未找到[无法处理请料]", true);
            ////_WcsLocSku.UpdateWcsLocSkuTask(skuIdB, abnormalCmdIdB);
            //abnormalCmdIdB = abnormalCmdIdB == 0 ? -3 : abnormalCmdIdB;
            //WriteLog($"ems创建任务开始157,abnormalCmdIdB{abnormalCmdIdB}", true);
            return result;
        }
        if (abnormalCmdIdA != 0 || abnormalCmdIdB != 0)
        {
            WriteLog($"ems创建任务开始161", true);
            result.CmdIdA = abnormalCmdIdA;
            result.CmdIdB = abnormalCmdIdB;
            return result;
        }

        //验证是否已生成EMS任务
        if (taskA?.TaskType == (int)EnumTaskType.EMS || taskA?.TaskType == (int)EnumTaskType.EmsEmpty || taskB?.TaskType == (int)EnumTaskType.EMS || taskB?.TaskType == (int)EnumTaskType.EmsEmpty)
        {
            WriteLog($"ems创建任务开始168", true);
            return result;
        }
        WriteLog($"ems创建任务开始171", true);
        var taskIdA = taskA?.Id ?? 0;
        var startLocA = taskA?.StartLoc ?? 0;
        var targetLocA = taskA?.TargetLoc ?? 0;
        var summaryIdA = taskA?.SummaryId ?? 0;
        var typeA = taskA?.TaskType ?? 0;
        var remarkUpA = taskA?.RemarkMemo ?? "";
        var targetEqA = taskA?.TargetEquip ?? 0;

        WriteLog($"ems创建任务开始173", true);
        var taskIdB = taskB?.Id ?? 0;
        var startLocB = taskB?.StartLoc ?? 0;
        var targetLocB = taskB?.TargetLoc ?? 0;
        var summaryIdB = taskB?.SummaryId ?? 0;
        var typeB = taskB?.TaskType ?? 0;
        var remarkUpB = taskB?.RemarkMemo ?? "";
        var targetEqB = taskB?.TargetEquip ?? 0;

        WriteLog($"ems创建任务开始189taskIdA{taskIdA},startLocA{startLocA},targetLocA{targetLocA},summaryIdA,{summaryIdA},typeA{typeA},remarkUpA{remarkUpA},targetEqA{targetEqA}", true);
        WriteLog($"ems创建任务开始190taskIdB{taskIdB},startLocB{startLocB},targetLocB{targetLocB},summaryIdB,{summaryIdB},typeB{typeB},remarkUpB{remarkUpB},targetEqB{targetEqB}", true);

        var AbnormalIdA = 0L;
        var AbnormalIdB = 0L;
        // 当前位置已经创建异常任务 todo 放下面
        if (typeA == (int)EnumTaskType.Abnormal && startLocA == locIdA)
        {
            WriteLog($"ems创建任务开始197", true);
            taskCmdA = _TaskCmd.GetTaskCmdByTaskId(taskIdA);
            if (taskCmdA is not null)
            {
                //WriteLog("ems创建任务开始102", true);
                AbnormalIdA = taskCmdA.Id.GetValueOrDefault();
            }
        }
        if (typeB == (int)EnumTaskType.Abnormal && startLocB == locIdB)
        {
            WriteLog($"ems创建任务开始207", true);
            taskCmdB = _TaskCmd.GetTaskCmdByTaskId(taskIdB);
            if (taskCmdB is not null)
            {
                //WriteLog("ems创建任务开始102", true);
                AbnormalIdB = taskCmdB.Id.GetValueOrDefault();
            }
        }
        if (AbnormalIdA + AbnormalIdB != 0)
        {
            result.CmdIdA = abnormalCmdIdA;
            result.CmdIdB = abnormalCmdIdB;
            return result;
        }



        //// 获取订单
        var summaryA = _WmsOutOrderSummary.GetOutOrderSummaryById(summaryIdA);
        if (taskIdA != 0 && summaryA == null)
        {
            WriteLog($"ems创建任务开始226", true);
            abnormalCmdIdA = SetAbnormal(locIdA, equipIdA, taskA, $"出库订单{summaryIdA}从表未找到{taskA?.TaskNo}.");
            //_WcsLocSku.UpdateWcsLocSkuTask(skuIdA, abnormalCmdIdA);
            abnormalCmdIdA = abnormalCmdIdA == 0 ? -3 : abnormalCmdIdA;
        }
        var summaryB = _WmsOutOrderSummary.GetOutOrderSummaryById(summaryIdB);
        if (taskIdB != 0 && summaryB == null)
        {
            WriteLog($"ems创建任务开始234", true);
            abnormalCmdIdB = SetAbnormal(locIdB, equipIdB, taskB, $"出库订单{summaryIdB}从表未找到{taskB?.TaskNo}.");
            //_WcsLocSku.UpdateWcsLocSkuTask(skuIdB, abnormalCmdIdB);
            abnormalCmdIdB = abnormalCmdIdB == 0 ? -3 : abnormalCmdIdB;
        }
        if (abnormalCmdIdA != 0 || AbnormalIdB != 0)
        {
            result.CmdIdA = abnormalCmdIdA;
            result.CmdIdB = abnormalCmdIdB;
            return result;
        }




        ////OLDTODO获取左右模（返回NULL 没有找到或设备异常）
        //var curingLR = _RcsEquipCuring.GetCuringRLByCuringId(targetEqA);

        //var curingCode = curingLR.CuringCode;
        //var mode = curingLR.Mode;
        //var modeIndex = curingLR.ModeIndex;

        // 获取可用小车数量小车
        //2007040101
        var targetLoc = targetLocA != 0 ? targetLocA : targetLocB;
        var equipEms = _RcsEquipEms.GetEquipEmssByTargetLoc(targetLoc);
        WriteLog($"ems创建任务开始257", true);
        if (equipEms.Count == 0)
        {
            WriteLog($"ems创建任务开始260", true);
            result.CmdIdA = -2;
            result.CmdIdB = -2;
            return result;
        }

        if (!equipEms.Where(t => t.Id == equipIdA || t.Id == equipIdB).Any())
        {
            WriteLog($"ems创建任务开始266", true);
            result.CmdIdA = -7;
            result.CmdIdB = -7;
            return result;
        }


        var emsIdA = equipIdA;
        var emsIdB = equipIdB;
        var wait = (int)EnumWait.NOPAIR;
        var curingA = _RcsEquipCuring.GetCuringByCuringId(targetEqA);
        var curingB = _RcsEquipCuring.GetCuringByCuringId(targetEqB);
        WriteLog($"ems创建任务开始276", true);
        var cmdA = new POWcsTaskCmd();
        var cmdB = new POWcsTaskCmd();

        if (equipEms.Count == 1) // 单车可用 查找这个车是A车还是B车
        {
            WriteLog($"ems创建任务开始282", true);
            //需要判断当前哪个
            //左2 右1 左B 右A
            //如果B车被禁用 所有B车任务下异常 重新请料

            //equipEms = _RcsEquipEms.GetEquipEmssByTargetLoc(targetLoc);


            //_RcsEquip.GetWorkEquipByIds
            var firstEms = equipEms.FirstOrDefault();
            var emsEquip = _RcsEquip.GetWorkEquipById(firstEms.Id);
            var equip = _RcsEquip.GetRcsEquipListByCode(emsEquip.EquipCode).Where(t => t.EquipEnable == 1).FirstOrDefault();
            var ems = _RcsEquipEms.GetEquipEmssById(equip?.Id ?? 0);

            WriteLog($"ems创建任务开始283--{ems?.EmsIndex ?? 0}", true);
            //如果是a车可用 则只能取a位置的车 无法取b位置
            if (ems.EmsIndex == 2)
            {
                WriteLog($"ems创建任务开始284", true);
                //生成a车任务 并修改为配对完成
                if (skuIdA != 0 && taskA is not null)
                {
                    wait = (int)EnumWait.NOPAIR;
                    // 创建任务
                    cmdA = new()
                    {
                        TaskId = taskIdA,
                        TaskNo = taskA.TaskNo,
                        TaskType = (int)EnumTaskType.EMS,
                        EquipId = emsIdA,
                        StartLoc = locIdA,
                        CurrentLoc = locIdA,
                        TargetLoc = targetLocA,
                        CmdStatus = (int)EnumTaskCmdStatus.Executable,
                        CmdType = (int)EnumTaskType.EMS,
                        PairWait = wait,
                    };
                    CreateEmsTaskCmd(cmdA, skuIdA);
                }
                else
                {
                    return result;
                }



            }
            else  //如果是b车可用 可以取 a b 位置的车 但是需要禁用最后一个硫化机
            {
                WriteLog($"ems创建任务开始318", true);
                //targetLoc 需再看
                //如果是设置的不可用硫化机 则生成异常任务
                var isTran = _WcsAbnormalEmsHalt.GetabnormalEmsHaltByEmsId(emsIdA).Where(t => t.CuringLocId == targetLocA).Any();
                if (isTran)
                {
                    abnormalCmdIdA = SetAbnormal(locIdA, equipIdA, taskA, $"{emsIdA}小车无法送到硫化机[{targetLocA}]");
                    //_WcsLocSku.UpdateWcsLocSkuTask(skuIdA, abnormalCmdIdA);
                    //_WmsOutOrderSummary.UpdateOutOrderSumaryRealNum(summaryA.Id.GetValueOrDefault());
                    abnormalCmdIdA = abnormalCmdIdA == 0 ? -3 : abnormalCmdIdA;
                    result.CmdIdA = abnormalCmdIdA;

                    return result;
                }
                //最多一次生成两个位置的ems任务
                //先创建本车的ems任务
                if (skuIdB != 0 && taskB is not null)
                {
                    // 创建任务
                    cmdB = new()
                    {
                        TaskId = taskIdB,
                        TaskNo = taskB.TaskNo,
                        TaskType = (int)EnumTaskType.EMS,
                        EquipId = emsIdB,
                        StartLoc = locIdB,
                        CurrentLoc = locIdB,
                        TargetLoc = targetLocB,
                        CmdStatus = (int)EnumTaskCmdStatus.Executable,
                        CmdType = (int)EnumTaskType.EMS,
                        PairWait = (int)EnumWait.COMPLETE,
                    };

                    CreateEmsTaskCmd(cmdB, skuIdB);


                }
                if (skuIdA != 0 && taskA is not null)
                {
                    WriteLog($"ems创建任务开始355", true);
                    // 创建任务
                    cmdA = new()
                    {
                        TaskId = taskA.Id,
                        TaskNo = taskA.TaskNo,
                        TaskType = (int)EnumTaskType.EMS,
                        EquipId = emsIdB,
                        StartLoc = locIdA,
                        CurrentLoc = locIdA,
                        TargetLoc = taskA.TargetLoc,
                        CmdStatus = (int)EnumTaskCmdStatus.Executable,
                        CmdType = (int)EnumTaskType.EMS,
                        PairWait = (int)EnumWait.COMPLETE,
                    };
                    CreateEmsTaskCmd(cmdA, skuIdA);
                }

            }
        }

        else if (equipEms.Count == 2)//两个车都可用的情况 考虑单胎
        {
            WriteLog($"ems创建任务开始378", true);
            //根据虚拟扫描查找该组号的出库任务
            //如果该组号数=1 则送单胎
            //如果该组号数=2 则看另一个位置组号是否一致
            //如果一致 生成指令 一起下发
            //如果不一致 

            // 获取小车负责取胎位和经过的扫描仪
            var netCuring = _WcsNetInCuring.GetWcsNetinCuringByPickupId(emsIdA);
            if (netCuring is null)
            {
                result.CmdIdA = -4;
                result.CmdIdB = -4;
                return result;
            }
            var scannerLoc = netCuring.ScannerLoc.GetValueOrDefault(); // 扫描仪位置

            //判断两个胎组号是否一致
            var groupA = taskA?.TaskGroup ?? 0;
            var groupB = taskB?.TaskGroup ?? 0;
            //如果组号一致 直接生成双车任务
            if (groupA == groupB)
            {
                WriteLog($"ems创建任务开始399", true);
                cmdA = new()
                {
                    TaskId = taskIdA,
                    TaskNo = taskA.TaskNo ?? 0,
                    TaskType = (int)EnumTaskType.EMS,
                    EquipId = emsIdA,
                    StartLoc = locIdA,
                    CurrentLoc = locIdA,
                    TargetLoc = targetLocA,
                    CmdStatus = (int)EnumTaskCmdStatus.Executable,
                    CmdType = (int)EnumTaskType.EMS,
                    PairWait = (int)EnumWait.COMPLETE,
                };



                cmdB = new()
                {
                    TaskId = taskIdB,
                    TaskNo = taskB.TaskNo ?? 0,
                    TaskType = (int)EnumTaskType.EMS,
                    EquipId = emsIdB,
                    StartLoc = locIdB,
                    CurrentLoc = locIdB,
                    TargetLoc = targetLocB,
                    CmdStatus = (int)EnumTaskCmdStatus.Executable,
                    CmdType = (int)EnumTaskType.EMS,
                    PairWait = (int)EnumWait.COMPLETE,
                };
                CreateEmsTaskCmd(cmdA, skuIdA);
                CreateEmsTaskCmd(cmdB, skuIdB);
            }
            else//当前组号不一致 可能是单胎
            {
                WriteLog($"ems创建任务开始434", true);
                //单胎情况
                if (skuIdA == 0 || skuIdB == 0)
                {
                    WriteLog($"ems创建任务开始438", true);
                    if (skuIdA != 0)//如果A车有胎 生成a车任务
                    {
                        WriteLog($"ems创建任务开始440", true);
                        cmdA = new()
                        {
                            TaskId = taskIdA,
                            TaskNo = taskA.TaskNo ?? 0,
                            TaskType = (int)EnumTaskType.EMS,
                            EquipId = emsIdA,
                            StartLoc = locIdA,
                            CurrentLoc = locIdA,
                            TargetLoc = targetLocA,
                            CmdStatus = (int)EnumTaskCmdStatus.Executable,
                            CmdType = (int)EnumTaskType.EMS,
                            PairWait = (int)EnumWait.COMPLETE,
                        };
                        CreateEmsTaskCmd(cmdA, skuIdA);

                    }
                    else if (skuIdB != 0)//如果b车有胎 生成b车任务 a车要空跑
                    {
                        WriteLog($"ems创建任务开始460", true);
                        cmdB = new()
                        {
                            TaskId = taskIdB,
                            TaskNo = taskB.TaskNo ?? 0,
                            TaskType = (int)EnumTaskType.EMS,
                            EquipId = emsIdB,
                            StartLoc = locIdB,
                            CurrentLoc = locIdB,
                            TargetLoc = targetLocB,
                            CmdStatus = (int)EnumTaskCmdStatus.Executable,
                            CmdType = (int)EnumTaskType.EMS,
                            PairWait = (int)EnumWait.COMPLETE,
                        };
                        CreateEmsTaskCmd(cmdB, skuIdB);
                        //如果b车跑单车 则给a车下一个可以到达的位置
                        var emptyTargetLoc = _RcsLocation.GetNextCuringLoc(targetLocB);
                        //没有a车可到达的位置 直接跳出
                        if (emptyTargetLoc is null)
                        {
                            return result;
                        }
                        cmdA = _Task.CreateEmsEmptyTaskAndCmd(locIdA, emptyTargetLoc?.Id ?? 0, emsIdA);


                    }
                }
                else //两个位置都有胎 但是组号不一致
                {
                    //判断两个位置的胎是否是拆单单胎输送
                    //summaryA.MasterId
                    var summaryCountA = _WmsOutOrderSummary.GetOutOrderSummaryByMasterId(summaryA.MasterId ?? 0).Count;
                    var summaryCountB = _WmsOutOrderSummary.GetOutOrderSummaryByMasterId(summaryB.MasterId ?? 0).Count;

                    if (summaryCountA == 1)
                    {
                        cmdA = new()
                        {
                            TaskId = taskIdA,
                            TaskNo = taskA.TaskNo ?? 0,
                            TaskType = (int)EnumTaskType.EMS,
                            EquipId = emsIdA,
                            StartLoc = locIdA,
                            CurrentLoc = locIdA,
                            TargetLoc = targetLocA,
                            CmdStatus = (int)EnumTaskCmdStatus.Executable,
                            CmdType = (int)EnumTaskType.EMS,
                            PairWait = (int)EnumWait.COMPLETE,
                        };

                        CreateEmsTaskCmd(cmdA, skuIdA);
                    }
                    else if (summaryCountB == 1)
                    {
                        cmdB = new()
                        {
                            TaskId = taskIdB,
                            TaskNo = taskB.TaskNo ?? 0,
                            TaskType = (int)EnumTaskType.EMS,
                            EquipId = emsIdB,
                            StartLoc = locIdB,
                            CurrentLoc = locIdB,
                            TargetLoc = targetLocB,
                            CmdStatus = (int)EnumTaskCmdStatus.Executable,
                            CmdType = (int)EnumTaskType.EMS,
                            PairWait = (int)EnumWait.COMPLETE,
                        };
                        //如果b车跑单车 则给a车下一个可以到达的位置
                        var emptyTargetLoc = _RcsLocation.GetNextCuringLoc(targetLocB);
                        //没有a车可到达的位置 直接跳出
                        if (emptyTargetLoc is null)
                        {
                            return result;
                        }
                        cmdA = _Task.CreateEmsEmptyTaskAndCmd(locIdA, emptyTargetLoc?.Id ?? 0, emsIdA);
                    }


                    //先判断外侧胎绑定时间 如果20s内 则不生成 防止后面的配对胎没有经过

                    WriteLog($"ems创建任务开始489", true);
                    //根据经过虚拟扫描的任务信息判断是否可以组成配对
                    //1、a车或b车是单胎 另一个位置可配对 则生成单车任务
                    //2、a车与b车都是单胎 生成双车任务
                    //3、a车与b车都可配对 生成双车任务(待定)
                    //注意生成双车任务时需要判断是否b车的目标位是否在a车的左侧 如果是 则生成单车任务

                    var groupCountA = _Task.GetTasksByGroupNoAndScanner(groupA, scannerLoc).Count();//.Where(t => t.TaskType == (int)EnumTaskType.Exwarehouse).Count();
                    var groupCountB = _Task.GetTasksByGroupNoAndScanner(groupB, scannerLoc).Count();//.Where(t => t.TaskType == (int)EnumTaskType.Exwarehouse).Count();
                    //两个位置在硫化排有配对的胎
                    if (groupCountA > 1 || groupCountB > 1)
                    {
                        WriteLog($"ems创建任务开始501", true);
                        //看a车下一个位置是否跟b车配对 
                        var isMatch = IsNextMatching(locIdA, scannerLoc, taskA.SpinTime ?? DateTime.MaxValue, groupB);
                        WriteLog($"ems创建任务开始504,是否配对:{isMatch}", true);
                        //如果a位置的下一条可以跟b配对 就送走单车任务
                        if (isMatch)
                        {
                            WriteLog($"ems创建任务开始508", true);
                            //生成A车的单车任务 B车不动

                            cmdA = new()
                            {
                                TaskId = taskIdA,
                                TaskNo = taskA.TaskNo ?? 0,
                                TaskType = (int)EnumTaskType.EMS,
                                EquipId = emsIdA,
                                StartLoc = locIdA,
                                CurrentLoc = locIdA,
                                TargetLoc = targetLocA,
                                CmdStatus = (int)EnumTaskCmdStatus.Executable,
                                CmdType = (int)EnumTaskType.EMS,
                                PairWait = (int)EnumWait.COMPLETE,
                            };
                            CreateEmsTaskCmd(cmdA, skuIdA);
                        }
                        //如果a位置的下一条不可以跟b配对 判断b位置的下一条是否跟a配对
                        else
                        {

                            WriteLog($"ems创建任务开始588", true);
                            //看B车下一个位置是否跟A车配对 否则双车走
                            isMatch = IsNextMatching(locIdB, scannerLoc, taskB.SpinTime ?? DateTime.MaxValue, groupA);
                            //如果a位置的下一条可以跟b配对 就送走单车任务
                            if (isMatch)
                            {
                                //生成B车的单车任务 A车为空车任务
                                cmdB = new()
                                {
                                    TaskId = taskIdB,
                                    TaskNo = taskB.TaskNo ?? 0,
                                    TaskType = (int)EnumTaskType.EMS,
                                    EquipId = emsIdB,
                                    StartLoc = locIdB,
                                    CurrentLoc = locIdB,
                                    TargetLoc = targetLocB,
                                    CmdStatus = (int)EnumTaskCmdStatus.Executable,
                                    CmdType = (int)EnumTaskType.EMS,
                                    PairWait = (int)EnumWait.COMPLETE,
                                };
                                CreateEmsTaskCmd(cmdB, skuIdB);
                                //如果b车跑单车 则给a车下一个可以到达的位置
                                var emptyTargetLoc = _RcsLocation.GetNextCuringLoc(targetLocB);
                                //没有a车可到达的位置 直接跳出
                                if (emptyTargetLoc is null)
                                {
                                    return result;
                                }
                                cmdA = _Task.CreateEmsEmptyTaskAndCmd(locIdA, emptyTargetLoc?.Id ?? 0, emsIdA);

                            }
                            else
                            {


                                WriteLog($"ems创建任务开始529", true);

                                var locA = _RcsLocation.GetLocationById(targetLocA);
                                var locB = _RcsLocation.GetLocationById(targetLocB);

                                if (locB.RelateScore <= locA.RelateScore)
                                //b车在a车的左边 生成双车任务
                                {
                                    WriteLog($"ems创建任务开始552", true);
                                    cmdA = new()
                                    {
                                        TaskId = taskIdA,
                                        TaskNo = taskA.TaskNo ?? 0,
                                        TaskType = (int)EnumTaskType.EMS,
                                        EquipId = emsIdA,
                                        StartLoc = locIdA,
                                        CurrentLoc = locIdA,
                                        TargetLoc = targetLocA,
                                        CmdStatus = (int)EnumTaskCmdStatus.Executable,
                                        CmdType = (int)EnumTaskType.EMS,
                                        PairWait = (int)EnumWait.COMPLETE,
                                    };
                                    cmdB = new()
                                    {
                                        TaskId = taskIdB,
                                        TaskNo = taskB.TaskNo ?? 0,
                                        TaskType = (int)EnumTaskType.EMS,
                                        EquipId = emsIdB,
                                        StartLoc = locIdB,
                                        CurrentLoc = locIdB,
                                        TargetLoc = targetLocB,
                                        CmdStatus = (int)EnumTaskCmdStatus.Executable,
                                        CmdType = (int)EnumTaskType.EMS,
                                        PairWait = (int)EnumWait.COMPLETE,
                                    };
                                    CreateEmsTaskCmd(cmdA, skuIdA);
                                    CreateEmsTaskCmd(cmdB, skuIdB);
                                }
                                else//b->a b距离分>a 不可生成双车任务
                                {
                                    //判断过虚拟扫描的数量决定生成哪一侧的任务
                                    var taskCountA = _Task.GetTasksByScannerAndEmsLoc(locIdA, scannerLoc).Count();
                                    var taskCountB = _Task.GetTasksByScannerAndEmsLoc(locIdB, scannerLoc).Count();
                                    //如果A的任务数更多 则生成A车的单车任务
                                    if (taskCountA >= taskCountB)
                                    {
                                        cmdA = new()
                                        {
                                            TaskId = taskIdA,
                                            TaskNo = taskA.TaskNo ?? 0,
                                            TaskType = (int)EnumTaskType.EMS,
                                            EquipId = emsIdA,
                                            StartLoc = locIdA,
                                            CurrentLoc = locIdA,
                                            TargetLoc = targetLocA,
                                            CmdStatus = (int)EnumTaskCmdStatus.Executable,
                                            CmdType = (int)EnumTaskType.EMS,
                                            PairWait = (int)EnumWait.COMPLETE,
                                        };
                                        CreateEmsTaskCmd(cmdA, skuIdA);
                                    }
                                    //如果B的任务数更多 则生成B车的单车任务 A车空跑
                                    else
                                    {
                                        WriteLog($"ems创建任务开始477", true);
                                        cmdB = new()
                                        {
                                            TaskId = taskIdB,
                                            TaskNo = taskB.TaskNo ?? 0,
                                            TaskType = (int)EnumTaskType.EMS,
                                            EquipId = emsIdB,
                                            StartLoc = locIdB,
                                            CurrentLoc = locIdB,
                                            TargetLoc = targetLocB,
                                            CmdStatus = (int)EnumTaskCmdStatus.Executable,
                                            CmdType = (int)EnumTaskType.EMS,
                                            PairWait = (int)EnumWait.COMPLETE,
                                        };
                                        CreateEmsTaskCmd(cmdB, skuIdB);
                                        //如果b车跑单车 则给a车下一个可以到达的位置
                                        var emptyTargetLoc = _RcsLocation.GetNextCuringLoc(targetLocB);
                                        //没有a车可到达的位置 直接跳出
                                        if (emptyTargetLoc is null)
                                        {
                                            return result;
                                        }
                                        cmdA = _Task.CreateEmsEmptyTaskAndCmd(locIdA, emptyTargetLoc?.Id ?? 0, emsIdA);

                                    }
                                }

                            }
                        }

                    }
                    //else if (groupCountB == 1 && groupCountA > 1) //B车不可配对 A车可以配对 
                    //{
                    //    WriteLog($"ems创建任务开始588", true);
                    //    //看B车下一个位置是否跟A车配对 否则双车走
                    //    var isMatch = IsNextMatching(locIdB, scannerLoc, taskB.SpinTime ?? DateTime.MaxValue, groupA);
                    //    //如果a位置的下一条可以跟b配对 就送走单车任务
                    //    if (isMatch)
                    //    {
                    //        //生成B车的单车任务 A车为空车任务
                    //        cmdB = new()
                    //        {
                    //            TaskId = taskIdB,
                    //            TaskNo = taskB.TaskNo ?? 0,
                    //            TaskType = (int)EnumTaskType.EMS,
                    //            EquipId = emsIdB,
                    //            StartLoc = locIdB,
                    //            CurrentLoc = locIdB,
                    //            TargetLoc = targetLocB,
                    //            CmdStatus = (int)EnumTaskCmdStatus.Executable,
                    //            CmdType = (int)EnumTaskType.EMS,
                    //            PairWait = (int)EnumWait.COMPLETE,
                    //        };
                    //        CreateEmsTaskCmd(cmdB, skuIdB);
                    //        //如果b车跑单车 则给a车下一个可以到达的位置
                    //        var emptyTargetLoc = _RcsLocation.GetNextCuringLoc(targetLocB);
                    //        //没有a车可到达的位置 直接跳出
                    //        if (emptyTargetLoc is null)
                    //        {
                    //            return result;
                    //        }
                    //        cmdA = _Task.CreateEmsEmptyTaskAndCmd(locIdA, emptyTargetLoc?.Id ?? 0, emsIdA);

                    //    }
                    //    //如果b位置的下一条不可以跟a配对 生成双车任务
                    //    //else
                    //    //{
                    //    //    WriteLog($"ems创建任务开始621", true);
                    //    //    //判断是否a车的目标位是否在b车的左侧 如果是 则生成单车任务
                    //    //    var locA = _RcsLocation.GetLocationById(targetLocA);
                    //    //    var locB = _RcsLocation.GetLocationById(targetLocB);
                    //    //    if (locB.RelateScore >= locA.RelateScore)//b车在a车的右边 则生成a
                    //    //    {
                    //    //        cmdA = new()
                    //    //        {
                    //    //            TaskId = taskIdA,
                    //    //            TaskNo = taskA.TaskNo ?? 0,
                    //    //            TaskType = (int)EnumTaskType.EMS,
                    //    //            EquipId = emsIdA,
                    //    //            StartLoc = locIdA,
                    //    //            CurrentLoc = locIdA,
                    //    //            TargetLoc = targetLocA,
                    //    //            CmdStatus = (int)EnumTaskCmdStatus.Executable,
                    //    //            CmdType = (int)EnumTaskType.EMS,
                    //    //            PairWait = (int)EnumWait.COMPLETE,
                    //    //        };
                    //    //        CreateEmsTaskCmd(cmdA, skuIdA);
                    //    //    }
                    //    //    else //生成双车任务
                    //    //    {
                    //    //        WriteLog($"ems创建任务开始645", true);
                    //    //        cmdA = new()
                    //    //        {
                    //    //            TaskId = taskIdA,
                    //    //            TaskNo = taskA.TaskNo ?? 0,
                    //    //            TaskType = (int)EnumTaskType.EMS,
                    //    //            EquipId = emsIdA,
                    //    //            StartLoc = locIdA,
                    //    //            CurrentLoc = locIdA,
                    //    //            TargetLoc = targetLocA,
                    //    //            CmdStatus = (int)EnumTaskCmdStatus.Executable,
                    //    //            CmdType = (int)EnumTaskType.EMS,
                    //    //            PairWait = (int)EnumWait.COMPLETE,
                    //    //        };
                    //    //        cmdB = new()
                    //    //        {
                    //    //            TaskId = taskIdB,
                    //    //            TaskNo = taskB.TaskNo ?? 0,
                    //    //            TaskType = (int)EnumTaskType.EMS,
                    //    //            EquipId = emsIdB,
                    //    //            StartLoc = locIdB,
                    //    //            CurrentLoc = locIdB,
                    //    //            TargetLoc = targetLocB,
                    //    //            CmdStatus = (int)EnumTaskCmdStatus.Executable,
                    //    //            CmdType = (int)EnumTaskType.EMS,
                    //    //            PairWait = (int)EnumWait.NOPAIR,
                    //    //        };
                    //    //        CreateEmsTaskCmd(cmdA, skuIdA);
                    //    //        CreateEmsTaskCmd(cmdB, skuIdB);
                    //    //    }
                    //    //}
                    //}


                    //else if (groupCountA == 1 && groupCountB == 1)//都不配对的情况  双车拉走 但是要看是否可送达 
                    else
                    {
                        WriteLog($"ems创建任务开始679", true);
                        //判断是否a车的目标位是否在b车的左侧 如果是 则生成单车任务
                        var locA = _RcsLocation.GetLocationById(targetLocA);
                        var locB = _RcsLocation.GetLocationById(targetLocB);
                        if (locB.RelateScore >= locA.RelateScore)//b车在a车的右边 则生成a
                        {
                            cmdA = new()
                            {
                                TaskId = taskIdA,
                                TaskNo = taskA.TaskNo ?? 0,
                                TaskType = (int)EnumTaskType.EMS,
                                EquipId = emsIdA,
                                StartLoc = locIdA,
                                CurrentLoc = locIdA,
                                TargetLoc = targetLocA,
                                CmdStatus = (int)EnumTaskCmdStatus.Executable,
                                CmdType = (int)EnumTaskType.EMS,
                                PairWait = (int)EnumWait.COMPLETE,
                            };
                            CreateEmsTaskCmd(cmdA, skuIdA);
                        }
                        else //生成双车任务
                        {
                            WriteLog($"ems创建任务开始702", true);
                            cmdA = new()
                            {
                                TaskId = taskIdA,
                                TaskNo = taskA.TaskNo ?? 0,
                                TaskType = (int)EnumTaskType.EMS,
                                EquipId = emsIdA,
                                StartLoc = locIdA,
                                CurrentLoc = locIdA,
                                TargetLoc = targetLocA,
                                CmdStatus = (int)EnumTaskCmdStatus.Executable,
                                CmdType = (int)EnumTaskType.EMS,
                                PairWait = (int)EnumWait.COMPLETE,
                            };
                            cmdB = new()
                            {
                                TaskId = taskIdB,
                                TaskNo = taskB.TaskNo ?? 0,
                                TaskType = (int)EnumTaskType.EMS,
                                EquipId = emsIdB,
                                StartLoc = locIdB,
                                CurrentLoc = locIdB,
                                TargetLoc = targetLocB,
                                CmdStatus = (int)EnumTaskCmdStatus.Executable,
                                CmdType = (int)EnumTaskType.EMS,
                                PairWait = (int)EnumWait.NOPAIR,
                            };
                            CreateEmsTaskCmd(cmdA, skuIdA);
                            CreateEmsTaskCmd(cmdB, skuIdB);
                        }
                    }


                }

            }
        }

        WriteLog("ems创建任务开始747", true);
        result.CmdIdA = cmdA?.Id ?? 0;
        result.CmdIdB = cmdB?.Id ?? 0;
        return result;
    }

    public int ExecuteTask(int taskNo)
    {
        //var taskCmd = _TaskCmd.GetExecutableTaskCmdByTaskNo(taskNo);
        var taskCmd = _TaskCmd.GetEmsExecutableTaskCmdByTaskNo(taskNo);
        if (taskCmd?.TaskId is not long taskId)
        {
            return 0;
        }
        var startLoc = taskCmd.StartLoc;
        var equipId = taskCmd.EquipId.GetValueOrDefault();

        // 获取ems 位置
        var targetLoc = _WcsEquipLoc.GetLocIdByEquipId(equipId);

        // 获取 skuID
        var task = _Task.GetTaskByTaskNo(taskNo);
        if (task?.SkuId is not long skuId)
        {
            return 0;
        }

        // 修改货位标识
        _RcsLocation.UpdateLocationAndWcsLocWithSkuId(skuId, startLoc, targetLoc);


        var num = _Task.UpdateTaskAndCmdStatus(targetLoc, taskId, (int)EnumTaskStatus.Executed);

        return num;
    }

    public int FinishTask(int taskNo)
    {
        var result = 0;
        try
        {
            WriteLog($"任务{taskNo}开始结束ems任务111", true);

            var task = _Task.GetExecutedByTaskNo(taskNo);
            //WriteLog($"任务{taskNo}开始结束ems任务452", true);


            if (task?.Id is not long taskId)
            {
                return 0;
            }
            var targetLoc = task.TargetLoc ?? 0;
            var equipId = task.StartEquip ?? 0;
            var directDelivery = task.DirectDelivery ?? 0;
            var taskC = _TaskCmd.GetTaskCmdByTaskId(taskId);
            if (taskC is not null)
            {

                equipId = taskC?.EquipId ?? 0;
            }
            // 获取ems 位置
            var startLoc = _WcsEquipLoc.GetLocIdByEquipId(equipId);
            WriteLog($"任务{taskNo}开始结束ems任务463", true);
            // 获取 skuID
            if (task?.SkuId is not long skuId)
            {
                return 0;
            }
            //var summaryId = task.SummaryId ?? 0;

            var summary = _WmsOutOrderSummary.GetOutOrderSummaryById(task.SummaryId ?? 0);
            var loc = summary?.TargetLoc ?? 0L;
            var summaryId = summary?.Id ?? 0L;


            // 同步MES
            try
            {
                WriteLog($"任务{taskNo}开始同步出库mes", true);
                if (task.TaskType != (int)EnumTaskType.EmsEmpty)
                {
                    SyncMes(skuId, summary, targetLoc);
                }
            }
            catch (Exception ex)
            {
                WriteLog($"任务{taskNo}同步出库mes异常,{ex.ToString()}", true);
            }


            //WriteLog($"任务{taskNo}开始结束ems任务464", true);
            // long.TryParse(summary?.Serno, out long midid);
            //var equip = _WcsEquipLoc.GetEquipIdByLocId(loc);
            ////WriteLog($"任务{taskNo}开始结束ems任务465", true);
            //var curing = _RcsEquipCuring.GetCuringById(equip);
            ////WriteLog($"任务{taskNo}开始结束ems任务466", true);
            //var isCuringCall = curing?.IsCuringCall ?? 0;
            //if (task?.TaskType != (int)EnumTaskType.Abnormal || summary?.Safe == 0 || (task?.TaskType == (int)EnumTaskType.Abnormal && isCuringCall != 0))
            //{
            var orderResult = _WmsOutOrderMaster.OutOrderFinish(task);
            // 出库单完成-备份

            // }
            WriteLog($"任务{taskNo}开始结束ems任务495,--{orderResult}", true);
            var locSkuTarget = targetLoc;
            if (task?.TaskType == (int)EnumTaskType.Abnormal)
            {
                locSkuTarget = 0; // 异常不需要绑定位置
            }
            WriteLog($"任务{taskNo}开始结束ems任务555", true);
            //修改货位标识

            var locResult = _RcsLocation.UpdateLocationAndWcsLocWithSkuId(skuId, startLoc, locSkuTarget);

            WriteLog($"任务{taskNo}开始结束ems任务504,startLoc--{startLoc},targetLoc--{targetLoc},--{locResult}", true);
            // 修改状态

            var taskResult = _Task.UpdateTaskAndCmdStatus(targetLoc, taskId, (int)EnumTaskCmdStatus.Finish);
            WriteLog($"任务{taskNo}开始结束ems任务505,--{taskResult}", true);
            // 备份删除
            try
            {
                _Task.BakAndDeleteTaskAndCmd(taskId);
            }
            catch (Exception ex)
            {
                WriteLog($"任务{taskNo}开始结束ems任务异常,--{ex.ToString()}", true);
            }



            //标记SKU数据
            result = _skuProduct.UpdateSkuProductOutBoundFlag(skuId);

            WriteLog($"任务{taskNo}开始结束ems任务511", true);


            //更新硫化机请料时间 通过目标硫化机位置获取硫化机id
            var curingId = _WcsEquipLoc.GetEquipIdByLocId(targetLoc);
            _RcsEquipCuring.UpdateCuringCallTime(curingId);


            //WriteLog($"任务{taskNo}开始结束ems任务520", true);



        }
        catch (Exception ex)
        {
            WriteLog($"开始结束ems任务异常{ex.ToString()}", true);
            return 0;
        }
        WriteLog($"任务{taskNo}开始结束ems任务523", true);
        return result;
    }






    /// <summary>
    /// 设置异常
    /// </summary>
    /// <param name="taskId">要删除的任务</param>
    /// <param name="locId">位置</param>
    /// <param name="skuId">prodsku</param>
    /// <param name="reamrk">备注</param>
    /// <returns></returns>
    private long SetAbnormal(long locId, long equipId, POWcsTask task, string reamrk, int taskNo = 0)
    {
        // 获取异常执行货叉
        //var netAbnormal = _WcsNetAbnormal.GetWcsNetAbnormalByStartLoc(locId);
        //var ems1 = netAbnormal?.Ems1 ?? 0;
        //var ems2 = netAbnormal?.Ems2 ?? 0;
        //var equipId = ems1;
        //if (_RcsEquip.GetWorkEquipById(ems1) is null)
        //{
        //    if (_RcsEquip.GetWorkEquipById(ems2) is not null)
        //    {
        //        equipId = ems2;
        //    }
        //    else
        //    {

        //        ////WriteLog($"两个小车{ems1},{ems2} 都不可用.");
        //        return 0;
        //    }
        //}

        var skuId = task?.SkuId ?? 0L;
        var taskId = task?.Id ?? 0L;
        var masterId = task?.MasterId ?? 0L;
        var summaryId = task?.SummaryId ?? 0L;
        var type = task?.TaskType ?? 0;

        // 无法送到进入异常口（结束当前任务、创建异常口任务）
        var abnormalTaskCmdId = _Abnormal.LocationAbnormalTaskAndCmdRetCmdId(locId, skuId, reamrk, taskNo, equipId);
        if (abnormalTaskCmdId > 0)
        {
            // 备份删除
            _Task.BakAndDeleteTaskAndCmd(taskId);

            var taskCmd = _TaskCmd.GetTaskCmdById(abnormalTaskCmdId);

            var newId = taskCmd?.TaskId ?? 0;
            var startLoc = taskCmd?.StartLoc ?? 0;
            var targetLoc = taskCmd?.TargetLoc ?? 0;

            _Task.UpdateTaskOrder(newId, masterId, summaryId);

            if (type != (int)EnumTaskType.Abnormal)
            {
                // 重新请料
                _WmsOutOrderSummary.UpdateOutOrderSumaryRealNum(summaryId);

                // 异常同步MES
                //var productCode = _ProdProduct.GetCodeBySkuId(skuId);

                // SyncMes(productCode, startLoc, targetLoc);
            }
            _WcsLocSku.UpdateWcsLocSkuTask(skuId, abnormalTaskCmdId);
            return abnormalTaskCmdId;
        }

        // 修改另一个货叉任务为 非等待
        //var netEms = _WcsAsrsNetOutPut.GetFirstAsrsNetOutputByStartLoc(locId);
        //if (netEms is not null)
        //{
        //    _TaskCmd.UpdateTaskCmdPairWait(netEms.EmsId.GetValueOrDefault(), 0);

        //    _TaskCmd.UpdateTaskCmdPairWait(netEms.EmsId2.GetValueOrDefault(), 0);
        //}

        return abnormalTaskCmdId;
    }

    /// <summary>
    /// 同步MES
    /// </summary>
    /// <param name="barCode"></param>
    /// <param name="startLoc"></param>
    /// <param name="taskId"></param>

    /// <summary>
    /// 同步MES
    /// </summary>
    /// <param name="skuId"></param>
    /// <param name="summaryId"></param>
    /// <param name="targetLoc"></param>
    private void SyncMes(long skuId, POWmsOutOrderSummary summary, long targetLoc)
    {
        WriteLog($"ems完成开始推送信息skuId-{skuId},summary{summary?.Id},targetloc{targetLoc}", true);
        var tireBarcode = _ProdProduct.GetCodeBySkuId(skuId);
        //WriteLog($"ems完成开始推送信息tireBarcode-{tireBarcode}", true);
        var curingRL = _RcsEquipCuring.GetCuringRLByCuringLocId(targetLoc);
        var equipNo = curingRL?.CuringCode;
        var model = curingRL?.Mode;
        //var equipModel = _RcsEquipCuring.GetCuringRLByMesModelRL(model);
        //var summmary = _WmsOutOrderSummary.GetOutOrderSummaryById(summaryId);
        var material = _ProdMaterial.GetMaterialById(summary?.MaterialId ?? 0);
        //    if (summary.CrnId == 10030901 || summary.CrnId == 10031001 || summary.CrnId == 10031101 || summary.CrnId == 10031201
        //|| summary.CrnId == 10031301 || summary.CrnId == 10031401 || summary.CrnId == 10031501 || summary.CrnId == 10031601)
        //    {
        //        if (model == "L")
        //        {
        //            model = "R";
        //        }
        //        else
        //        {
        //            model = "L";
        //        }
        //    }


        var mesService = K8.RSF.Client.Create<IMesService>();
        EndParam param = new EndParam()
        {
            TcmNo = equipNo,
            TcmDir = model,
            TcmSpec = material?.MaterialCode,
            TcmSpecDesc = material?.MaterialCode,
            TirCode = tireBarcode,
            Angle = 0,
            TbmPs = summary.TbmPs ?? "",
            CompFlag = "E",
            //TbmSpec=mesproduct?.TbmSpec
        };
        mesService.AddOutEnd(param);

    }

    /// <summary>
    /// 获取位置绑定的任务
    /// </summary>
    /// <param name="locId"></param>
    /// <returns></returns>
    private POWcsTask GetTaskByLoc(long skuId)
    {
        // 查找主任务
        var task = _Task.GetTaskBySkuId(skuId);
        if (task == null)
        {
            return null;
        }
        return task;
    }

    private POWcsLocSku GetWcsLocSkuByLocId(long locId)
    {
        // 查找当前Sku
        var wcsLocSku = _WcsLocSku.GetWcsLocSkuNoTaskByLocId(locId);
        return wcsLocSku;
    }

    /// <summary>
    /// 下一个硫化胎胚是否和当前胎胚匹配
    /// </summary>
    /// <param name="scannerLoc">扫描仪</param>
    /// <param name="otherLoc">旁边的ems取货位</param>
    /// <param name="otherScannerTime">旁边的ems取货位的扫描时间</param>
    ///  <param name="taskGroup">自己组号</param>
    /// <returns>是否匹配[true 匹配；false 不匹配]</returns>
    private bool IsNextMatching(long otherLoc, long scannerLoc, DateTime otherScannerTime, int taskGroup)
    {
        // 和下一个任务目标位置
        var task = _Task.GetTasksByScannerAndTime(otherLoc, scannerLoc, otherScannerTime);
        return task?.TaskGroup == taskGroup;

    }

    public long CreateEmsTaskCmd(POWcsTaskCmd cmdBean, long skuId)
    {
        var cmd = _TaskCmd.CreateTaskCmd(cmdBean);
        //先创建指令 成功后在修改任务类型
        if (cmd is null)
        {
            return (0);
        }
        _Task.EditTaskType(cmdBean.TaskId ?? 0, (int)EnumTaskType.EMS, cmdBean.EquipId ?? 0);

        // 修改位置赋值任务
        var result = _WcsLocSku.UpdateWcsLocSkuTask(skuId, cmdBean.TaskId ?? 0);
        return cmd.Id ?? 0;
    }





}